package com.hsgene.user.service.impl;

import com.hsgene.user.condition.CompanyCondition;
import com.hsgene.user.domain.Company;
import com.hsgene.user.dto.orginazation.CompanyDto;
import com.hsgene.user.persistence.CompanyRepository;
import com.hsgene.user.persistence.UserRepository;
import com.hsgene.user.service.CompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.order.service.impl
 * @author: maodi
 * @createDate: 2018/11/5 10:13
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    CompanyRepository companyRepository;

    @Resource
    UserRepository userRepository;

    /**
     * @param condition 公司条件
     * @return int
     * @description 公司条件查询数量
     * @author maodi
     * @createDate 2018/11/6 16:34
     */
    @Override
    public int queryCompanyCountByCondition(CompanyCondition condition) {
        return companyRepository.queryCompanyCountByCondition(condition);
    }

    /**
     * @param condition 公司条件
     * @return java.util.List<com.hsgene.user.domain.Company>
     * @description 公司条件查询
     * @author maodi
     * @createDate 2018/11/6 16:38
     */
    @Override
    public List<CompanyDto> queryCompanyByCondition(CompanyCondition condition) {
        return companyRepository.queryCompanyByCondition(condition);
    }

    /**
     * @param id 公司id
     * @return java.util.List<com.hsgene.user.domain.Company>
     * @description 公司根据id查询
     * @author maodi
     * @createDate 2018/11/6 16:38
     */
    @Override
    public List<Company> queryCompanyById(String id) {
        return companyRepository.queryCompanyById(id);
    }

    /**
     * @param companyName 公司名字
     * @return int
     * @description 公司名字查询数量
     * @author maodi
     * @createDate 2018/11/6 18:01
     */
    @Override
    public int queryCompanyCountByName(String companyName) {
        return companyRepository.queryCompanyCountByName(companyName);
    }

    /**
     * @param companyName 公司名称
     * @param companyId   公司id
     * @return int
     * @description 公司名字除本身外查询数量
     * @author maodi
     * @createDate 2018/11/7 15:04
     */
    @Override
    public int queryCompanyCountByNameExcludeSelf(String companyName, String companyId) {
        return companyRepository.queryCompanyCountByNameExcludeSelf(companyName, companyId);
    }

    /**
     * @param
     * @return java.util.List<Map<String,Object>>
     * @description 公司名字下拉框数据源
     * @author maodi
     * @createDate 2018/11/7 9:10
     */
    @Override
    public List<Map<String, Object>> selectCompany() {
        return companyRepository.selectCompany();
    }

    /**
     * @param company 公司
     * @return void
     * @description 公司新增
     * @author maodi
     * @createDate 2018/11/6 16:39
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int insertCompany(Company company) {
        return companyRepository.insertCompany(company);
    }

    /**
     * @param company 公司
     * @return void
     * @description 公司修改
     * @author maodi
     * @createDate 2018/11/6 16:40
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int updateCompany(Company company) {
        userRepository.updateCompanyNameByCompanyId(company.getCompanyName(), company.getId());
        return companyRepository.updateCompany(company);
    }

    /**
     * @param company 公司
     * @return void
     * @description 公司删除
     * @author maodi
     * @createDate 2018/11/6 16:40
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteCompany(Company company) {
        return companyRepository.deleteCompany(company);
    }

    /**
     * @param companyIds 公司id数组
     * @return int
     * @description 公司批量删除
     * @author maodi
     * @createDate 2018/11/13 17:04
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteCompanyMulti(String[] companyIds) {
        return companyRepository.deleteCompanyMulti(companyIds, new Date());
    }

    /**
     * @param companyId 公司id
     * @return java.lang.Integer
     * @description 查询公司id在系统用户表中使用数量
     * @author maodi
     * @createDate 2018/11/29 11:34
     */
    @Override
    public Integer queryOperationAccountCountByCompanyId(String companyId) {
        return companyRepository.queryOperationAccountCountByCompanyId(companyId);
    }
}
